package com.lemon.smartsafety.utils;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;

import com.lemon.smartsafety.utils.common.LoggerHelper;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cn.finalteam.galleryfinal.permission.EasyPermissions;

/**
 * 项目名称：up_exec_law
 * 类描述：
 * 创建人：Fargo
 * 创建时间：2016/6/22 9:55
 * 修改人：Fargo
 * 修改时间：2016/6/22 9:55
 * 修改备注：
 */
public class RulesUtils {


    /**
     * 验证电话号码是否合法
     *
     * @param phoneNumber 手机号
     * @return 是否合法
     */
    public static boolean isPhoneNumberValid(String phoneNumber) {
        boolean isValid = false;
        String expression = "((^(13|14|15|18|17)[0-9]{9}$)|(^0[1,2]{1}\\d{1}-?\\d{8}$)|(^0[3-9] {1}\\d{2}-?\\d{7,8}$)|(^0[1,2]{1}\\d{1}-?\\d{8}-(\\d{1,4})$)|(^0[3-9]{1}\\d{2}-? \\d{7,8}-(\\d{1,4})$))";
        CharSequence inputStr = phoneNumber;
        Pattern pattern = Pattern.compile(expression);
        Matcher matcher = pattern.matcher(inputStr);
        if (matcher.matches()) {
            isValid = true;
        }
        return isValid;
    }


    /*
     * 验证号码 手机号 固话均可
     */
    public static boolean isPhoneOrPhoneValid(String phoneNumber) {
        boolean isValid = false;

        // String expression =
        // "((^(13|15|18)[0-9]{9}$)|(^0[1,2]{1}\\d{1}-?\\d{8}$)|(^0[3-9] {1}\\d{2}-?\\d{7,8}$)|(^0[1,2]{1}\\d{1}-?\\d{8}-(\\d{1,4})$)|(^0[3-9]{1}\\d{2}-? \\d{7,8}-(\\d{1,4})$))";
        // 手机号
        String expression = "(^(0|86|17951)?(13([0-9])|15([012356789])|17([678])|18([0-9])|14([57]))([0-9]){8})";
        // 400 800
        // String expression2 = "(^([48])00\\d)";
        // 固话
        // String expression3 = "((\\d{2,5}\\d{7,8}))";
        // String expression3 = "((\\d{2,5}-\\d{7,8}))";
        CharSequence inputStr = phoneNumber;

        Pattern pattern = Pattern.compile(expression);
        // Pattern pattern2 = Pattern.compile(expression2);
        // Pattern pattern3 = Pattern.compile(expression3);

        Matcher matcher = pattern.matcher(inputStr);
        // Matcher matcher2 = pattern2.matcher(inputStr);
        // Matcher matcher3 = pattern3.matcher(inputStr);

        // if (matcher.matches() || matcher2.matches() || matcher3.matches()) {
        if (matcher.matches()) {
            isValid = true;
        }

        return isValid;

    }


    /**
     * 验证邮箱输入是否合法
     *
     * @param strEmail 邮箱地址
     * @return boolean 是否合法
     */
    public static boolean isEmail(String strEmail) {
        // String strPattern =
        // "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        String strPattern = "^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$";

        Pattern p = Pattern.compile(strPattern);
        Matcher m = p.matcher(strEmail);
        return m.matches();
    }


    public boolean isChinese(char c) {

        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);

        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {

            return true;

        }

        return false;

    }

    public static boolean checkData(String start, String end) {
        String s = start.replace("-", "");
        String e = end.replace("-", "");
        LoggerHelper.error(RulesUtils.class, Integer.parseInt(s) + "==" + Integer.parseInt(e));

        if (Integer.parseInt(s) >= Integer.parseInt(e)) {
            return true;
        }
        return false;
    }


    final public static int REQUEST_CODE_ASK_CALL_PHONE = 123;

    final public static int REQUEST_CODE_ASK_CAMERA = 12321;

    /**
     * 获取是否拥有相机权限
     *
     * @param mContext
     * @return
     */
    public static boolean getOpenCameraPermission(final Activity mContext) {
        if (Build.VERSION.SDK_INT >= 23) {
            int checkCallPhonePermission = ContextCompat.checkSelfPermission(mContext, Manifest.permission.CAMERA);
            if (checkCallPhonePermission != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(mContext, new String[]{Manifest.permission.CAMERA}, REQUEST_CODE_ASK_CAMERA);
                return false;
            } else {
                return true;
            }
        } else {
            return true;
        }
    }


    public static boolean getOpenCameraEasyPermissions(final Activity mContext) {
        if (EasyPermissions.hasPermissions(mContext, Manifest.permission.CAMERA)) {
            // Have permission, do the thing!
            return true;
        } else {
            // Ask for one permission
            EasyPermissions.requestPermissions(mContext, "需要请求camera权限",
                    REQUEST_CODE_ASK_CAMERA, Manifest.permission.CAMERA);
            return false;
        }
    }


    public static final String[] mFchar = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};

    public static String getFristChar(String strF) {

        String charIndex = strF;

        for (int i = 0; i < mFchar.length; i++) {
            if (charIndex.equals(mFchar[i])) {
                return charIndex;
            }
        }


        return "#";
    }


}
